//
//  ChatConversationListViewController.m
//  RZIM
//
//  Created by reyzhang on 2021/8/12.
//

#import "ChatConversationListViewController.h"
#import "RZSlideMenuView.h"
#import <Masonry/Masonry.h>
#import "CreateGroupViewController.h"
#import "UIView+Helper.h"
#import "ContactViewController.h"
#import "UIViewController+HUD.h"
#import <YMIMKit/YMIMKit.h>
#import "AddFriendViewController.h"
#import "ChatConversationViewController.h"
#import "ChatEmptyView.h"

@interface ChatConversationListViewController ()
@property (nonatomic, strong) UIButton *moreBtn;
@property (nonatomic, strong) RZSlideMenuView *menuView;
@property (nonatomic,strong)NSMutableArray *userArray;

@property (nonatomic,strong)ChatEmptyView *emptyView;
@end

@implementation ChatConversationListViewController

- (id)init {
    self = [super init];
    if (self) {
        //设置要显示的会话类型
        self.displayConversationTypeArray = @[@(ConversationType_PRIVATE),
                                              @(ConversationType_GROUP),
                                              @(ConversationType_CHATROOM)];
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:self.moreBtn];
    self.navigationItem.rightBarButtonItem = right;
    
//    self.conversationListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.conversationListTableView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    [self setEmptyView];
  
  
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)startShowHUD {
    [self showLoading];
    
}

- (void)endHideHUD {
    [self hideHud];
}

-(void)setEmptyView{
    
    [self.view addSubview:self.emptyView];
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
            
        make.centerY.mas_equalTo(self.view.mas_centerY);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        
    }];

    self.emptyConversationView = self.emptyView;
}

#pragma mark Private method
- (void)openMenu:(id)sender  {
    RZSlideMenuConfig *config = [[RZSlideMenuConfig alloc] init];
    config.direction = DirectionFromRight;
    self.menuView = [[RZSlideMenuView alloc] initWithConfig:config];
    
    RZSlideMenuModel *chat = [RZSlideMenuModel createWithIcon:@"Menu_Chat" name:@"发起会话"];
    RZSlideMenuModel *addfriend = [RZSlideMenuModel createWithIcon:@"Menu_Add" name:@"添加好友"];
    RZSlideMenuModel *groupchat = [RZSlideMenuModel createWithIcon:@"Menu_GroupChat" name:@"发起群聊"];
    RZSlideMenuModel *roomchat = [RZSlideMenuModel createWithIcon:@"Menu_GroupChat" name:@"新建聊天室"];
    self.menuView.menuArray = @[chat,addfriend,groupchat,roomchat];
    
    
    [self.menuView showWithAnimation:YES];
    __weak __typeof(self) weakSelf = self;
    
    self.menuView.selectMenuAtIndex = ^(NSInteger index, RZSlideMenuModel * _Nonnull model) {
//        @strongify(self);
        [weakSelf handlerSlideMenuAtIndex:index];
    };
}


/**
 处理侧滑菜单
 */
- (void)handlerSlideMenuAtIndex:(NSInteger)index {
    if (index == 0) {
        
        ContactViewController *vc = [[ContactViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.showSelect = YES;
        vc.showSingleSelect = YES;
        vc.title = @"发起会话";
        vc.didSelectResultBlock = ^(NSArray * _Nonnull userArray) {
            UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            CreateGroupViewController *vc = [main instantiateViewControllerWithIdentifier:@"CreateGroupViewController"];
            vc.hidesBottomBarWhenPushed = YES;
            vc.uidsList = userArray;
          
            [self.navigationController pushViewController:vc animated:YES];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if (index == 1){
        
        AddFriendViewController *vc = [[AddFriendViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (index==2){
        
        ContactViewController *vc = [[ContactViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.showSelect = YES;
        vc.title = @"发起群聊";
        vc.didSelectResultBlock = ^(NSArray * _Nonnull userArray) {
            UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            CreateGroupViewController *vc = [main instantiateViewControllerWithIdentifier:@"CreateGroupViewController"];
            vc.hidesBottomBarWhenPushed = YES;
            vc.uidsList = userArray;
            vc.title = @"创建群聊";
            [self.navigationController pushViewController:vc animated:YES];
        };
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (index==3){
        
        ContactViewController *vc = [[ContactViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.showSelect = YES;
        vc.title = @"新建聊天室";
        vc.didSelectResultBlock = ^(NSArray * _Nonnull userArray) {
            UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            CreateGroupViewController *vc = [main instantiateViewControllerWithIdentifier:@"CreateGroupViewController"];
            vc.hidesBottomBarWhenPushed = YES;
            vc.uidsList = userArray;
            vc.title = @"新建聊天室";
            vc.creteType = RoomType;
            [self.navigationController pushViewController:vc animated:YES];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

/**
 table cell was clicked
 */
- (void)onSelectedTableRow:(UITableView *)tableView conversationModel:(YMConversation *)model atIndexPath:(NSIndexPath *)indexPath {
    if (self.useSelect) {
        !self.didSelectItemBlock ?: self.didSelectItemBlock(model);
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        ChatConversationViewController *vc = [[ChatConversationViewController alloc] initWithConversationType:model.groupInfo.groupType targetId:model.targetId];
        vc.hidesBottomBarWhenPushed = YES;
        vc.title = model.groupInfo.groupType == 0 ? model.userList.firstObject.userName : model.groupInfo.groupName;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (UIButton *)moreBtn {
    if (!_moreBtn) {
        _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreBtn setImage:[UIImage imageNamed:@"Message_Plus"] forState:UIControlStateNormal];
        _moreBtn.frame = CGRectMake(0, 0, 40, 40);
        [_moreBtn addTarget:self action:@selector(openMenu:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreBtn;
}

-(ChatEmptyView *)emptyView{
   
    if (_emptyView == nil) {
        
        _emptyView = [[ChatEmptyView alloc]init];
     
    }
    
    return _emptyView;
    
}
@end
